package com.project.client.service.impl;

import com.project.client.domain.AreaInfoEntity;
import com.project.client.domain.CentigradeRecordEntity;
import com.project.client.dto.CentigradeDTO;
import com.project.client.mapper.CentigradeRecordMapper;
import com.project.client.service.AreaInfoService;
import com.project.client.service.CentigradeRecordService;
import com.project.common.exception.CustomException;
import com.project.common.service.BaseServiceImpl;
import com.project.common.utils.ConvertUtils;
import com.project.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 温度记录数据Service业务层处理
 *
 * @author project
 * @date 2021-05-10
 */
@Service
public class CentigradeRecordServiceImpl extends BaseServiceImpl<CentigradeRecordMapper, CentigradeRecordEntity> implements CentigradeRecordService {

    @Autowired
    private AreaInfoService areaInfoService;

    @Override
    public List<CentigradeDTO> findClientData(String date, String areaKey) {
        if ("china".equals(areaKey)) {
            return baseDao.selectChinaData(date);
        } else {
            return baseDao.selectData(date, areaKey);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String importData(List<CentigradeRecordEntity> list) {
        int cc = 0;
        for (CentigradeRecordEntity entity : list) {
            if (entity.getCentigrade() != null) {
                AreaInfoEntity area = areaInfoService.selectByName(entity.getAreaName());
                if (area == null) continue;
                entity.setAreaCode(area.getAreaCode());
                int count = save(entity);
                if (count <= 0) {
                    throw new CustomException("导入失败");
                }
                cc++;
            }
        }
        return "恭喜您，数据已全部导入成功！共 " + cc + " 条";
    }

    @Override
    public CentigradeRecordEntity selectById(Long id) {
        return baseDao.selectById(id);
    }

    @Override
    public List<CentigradeRecordEntity> selectList(CentigradeRecordEntity entity) {
        List<CentigradeRecordEntity> list = baseDao.list(entity);
        return list;
    }

    @Override
    public int save(CentigradeRecordEntity entity) {
        CentigradeRecordEntity old = baseDao.selectByAlike(entity);
        if (old != null) {
            throw new CustomException(String.format("%s - %s 已存在数据", old.getAreaName(), DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD, old.getRecordDate())));
        }
        int count = baseDao.insert(entity);
        return count;
    }

    @Override
    public int update(CentigradeRecordEntity entity) {
        int count = baseDao.updateById(entity);
        return count;
    }

    @Override
    public int delete(Long[] ids) {
        int count = baseDao.deleteBatchIds(Arrays.asList(ids));
        return count;
    }
}
